#include<iostream>
#include<pthread.h>
#include<unistd.h>

pthread_mutex_t mutex;
pthread_cond_t cond;
int cnt=0;
void* count(void* args)
{
    uint64_t number=(uint64_t)args;
    pthread_detach(pthread_self());
    std::cout<<"pthread:"<<number<<"create success"<<std::endl;

    while(true)
    {
        pthread_mutex_lock(&mutex);//先加索
        //再等待
        pthread_cond_wait(&cond,&mutex);//在等待时 会自动释放锁
        std::cout<<"pthread: "<<number<<",cnt: "<<cnt<<std::endl;
        pthread_mutex_unlock(&mutex);//解锁
        //sleep(3);
    }
}

int main()
{
    for(uint64_t i=0;i<5;i++)
    {
        pthread_t tid;
        pthread_create(&tid,nullptr,count,(void*)&i);
    }
    std::cout<<"main thread ctrl begin: "<<std::endl;
    while(true)
    {
        sleep(1);
        pthread_cond_signal(&cond);//唤醒在cond的等待队列中等待的一个线程，默认都是第一个
        std::cout<<"signal one thread..."<<std::endl;
    }
}